<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="70%" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="top">
            <el-form-item label="业务员" prop="profession">
                <el-container>
                    <el-aside width="200px" v-loading="showGrouploading">
                        <el-container>
                            <el-header>
                                <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
                            </el-header>
                            <el-main class="nopadding">
                                <el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
                            </el-main>
                        </el-container>
                    </el-aside>
                    <el-container>
                        <el-header>
                            <div class="right-panel">
                                <div class="right-panel-search">
                                    <el-input v-model="search.keyword" placeholder="登录账号 / 姓名" clearable></el-input>
                                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                                </div>
                            </div>
                        </el-header>
                        <el-main class="nopadding">
                            <scTable ref="table" :apiObj="apiObj" @select-all="selectAll" @selection-change="selectionChange" stripe remoteSort remoteFilter height="600" hideDo border>
                                <el-table-column type="selection" width="50"></el-table-column>
                                <el-table-column label="ID" prop="id" width="80"></el-table-column>
                                <el-table-column label="头像" width="80">
                                    <template #default="scope">
                                        <el-avatar :src="scope.row.avatar" size="small"></el-avatar>
                                    </template>
                                </el-table-column>
                                <el-table-column label="登录账号" prop="account" width="150"></el-table-column>
                                <el-table-column label="姓名" prop="name" width="150"></el-table-column>
                                <el-table-column label="所属角色" prop="role_name" width="200"></el-table-column>
                                <el-table-column label="所属部门" prop="dept_name" width="200"></el-table-column>
                                <el-table-column label="所属岗位" prop="jobs_name" width="200"></el-table-column>
                                <el-table-column label="加入时间" prop="create_time" width="170"></el-table-column>
                            </scTable>
                        </el-main>
                    </el-container>
                </el-container>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">提 交</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增客户',
				edit: '分配客户',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:[],
				admin_id: "",
			},
			//验证规则
			rules: {
			},
            showGrouploading: false,
			groupFilterText: '',
			group: [],
			apiObj: this.$API.system.admin.list,
            search: {
				keyword: null
			}
		}
	},
	mounted() {
        this.getGroup()
	},
    watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            if(this.form.admin_id == ''){
                this.$message.error("请选择业务员")
            }else{
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = [];
                        res = await this.$API.client.apply.check.post(this.form);
                        this.isSaveing = false;
                        if (res.code == 1) {
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        } else {
                            this.$alert(res.message, "提示", { type: 'error' })
                        }
                    } else {
                        return false;
                    }
                })
            }
		},
		//表单注入数据
		setData(data) {
            this.form.id = data
		},
        selectAll() {
            this.$message.error("请选择一条数据！")
            this.$refs.table.clearSelection()
        },
        //表格选择后回调事件
		selectionChange(selection) {
            if(selection.length > 1){
                const del_row = selection.shift()
                this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
            }
			this.form.admin_id = selection[0].id;
		},
        upsearch() {
			this.$refs.table.upData(this.search)
		},
		//加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.system.dept.list.get();
			this.showGrouploading = false;
			var allNode = { id: '', label: '所有' }
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			var params = {
				groupId: data.id
			}
			this.$refs.table.reload(params)
		},
	}
}
</script>

<style></style>
